package com.zpl.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpl.goods.entity.Category;
import com.zpl.goods.entity.CategoryUS;
import com.zpl.goods.entity.vo.CategoryVO;
import com.zpl.goods.mapper.CategoryMapper;
import com.zpl.goods.mapper.CategoryUSMapper;
import com.zpl.goods.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangyuxin
 * @date 2020/9/29
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private CategoryUSMapper categoryUSMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisTemplate<String, Serializable> redisCacheTemplate;


    @Override
    @CacheEvict(value="category",key="'categories'")
    public boolean insert(Category category) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_name",category.getCategoryName());
        List<Category> categories = categoryMapper.selectList(queryWrapper);
        if (categories.size()>0){
            return false;
        }
        categoryMapper.insert(category);
        return true;
    }

    @Override
    @CacheEvict(value="category",key="'categories'")
    public boolean delete(Long id) {
        if(categoryUSMapper.deleteById(id)>0){
            return true;
        }
        return false;
    }

    @Override
    @CacheEvict(value="category",key="'categories'")
    public boolean update(CategoryUS categoryUS) {
        System.out.println(JSON.toJSON(categoryUS));
        if (categoryUSMapper.updateById(categoryUS) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public CategoryUS selectById(Long id) {
        return categoryUSMapper.selectById(id);
    }


    @Override
    public List<CategoryVO> selectByParentId(Long parentId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("parent_id",parentId);
        List<Category> categoryList=categoryMapper.selectList(queryWrapper);
        List<CategoryVO> categoryVOList =new ArrayList<>();
        for(int i=0;i<categoryList.size();i++){
            CategoryVO categoryVO = new CategoryVO();
            categoryVO.setCategoryName(categoryList.get(i).getCategoryName());
            categoryVO.setId(categoryList.get(i).getXid());
            categoryVO.setStatus(categoryList.get(i).getStatus());
            List<CategoryVO> categoryVOList1 = selectByParentId(categoryList.get(i).getXid());
            if(categoryVOList1.size()>0){
                categoryVO.setChildren(categoryVOList1);
            }
            categoryVOList.add(categoryVO);
        }
        return categoryVOList;
    }

    @Override
    @Cacheable(value="category",key="'categories'")
    public List<CategoryVO> show() {
        List<CategoryVO> list = selectByParentId(0L);
        return list;
    }
}
